//
//  StringExtension.swift
//  test
//
//  Created by 苏泊尔漫也要按时吃饭 on 2022/12/5.
//

import Foundation

extension String {

    func isvaliteMobile() -> Bool {
        let mobileRegex = "^((13[0-9])|(15[^4,\\D])|(18[0,0-9])|(17[0,0-9]))\\d{8}$"
        let mobileTest:NSPredicate = NSPredicate(format: "SELF MATCHES %@", mobileRegex)
        return mobileTest.evaluate(with: self)
    }
    
    func isvaliteEmail() -> Bool {
        let emailRegex: String = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
        let emailTest:NSPredicate = NSPredicate(format: "SELF MATCHES %@", emailRegex)
        return emailTest.evaluate(with: self)
    }
    
    func isvaliteidNum() -> Bool {
        let idRegex = "^(\\d{14}|\\d{17})(\\d|[xX])$"
        let idTest:NSPredicate = NSPredicate(format: "SELF MATCHES %@", idRegex)
        return idTest.evaluate(with: self)
    }
    
    func isvaliteUserName() -> Bool {
        let userNameRegex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,20}$"
        let idTest:NSPredicate = NSPredicate(format: "SELF MATCHES %@", userNameRegex)
        return idTest.evaluate(with: self)
    }
    
}
